# Maintainer: Marcelo Duarte https://github.com/marcelotduarte

_realname=lief
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgdesc="LIEF - Library to Instrument Executable Formats (mingw-w64)"
pkgver=0.16.5
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/lief-project/LIEF/"
msys2_references=(
  'purl: pkg:pypi/lief'
)
license=('spdx:Apache-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-mbedtls"
  "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
  "${MINGW_PACKAGE_PREFIX}-spdlog"
  "${MINGW_PACKAGE_PREFIX}-tl-expected"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-ccache"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-nanobind"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-cattrs"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-packaging"
  "${MINGW_PACKAGE_PREFIX}-python-pathspec"
  "${MINGW_PACKAGE_PREFIX}-python-pydantic"
  "${MINGW_PACKAGE_PREFIX}-python-pydantic-core"
  "${MINGW_PACKAGE_PREFIX}-python-pyproject-metadata"
  "${MINGW_PACKAGE_PREFIX}-python-rich"
  "${MINGW_PACKAGE_PREFIX}-python-scikit-build-core"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-tomli"
)
options=('!strip')
source=(
  https://github.com/lief-project/LIEF/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
  001-fix-setup.patch
  002-fix-header.patch
)
sha256sums=(
  '10ef46bc958d7936feb155040c874504ab0bd40dc59b4678f807691ccd0d138f'
  '8cab3f9aa2d010232b8d782bc09be0136752bd5f41b5d8a4dae976ac4b47d9a2'
  '6f75648988a145326c46cb1017bdc56cd957a7301d97871d4f8af8cc25a954e8'
)

prepare() {
  pushd "LIEF-${pkgver}"
  patch -p0 -i ../001-fix-setup.patch
  patch -p0 -i ../002-fix-header.patch
  popd

  rm -rf build-${MSYSTEM} | true
  cp -r "LIEF-${pkgver}" "build-${MSYSTEM}"
}

build() {
  cd build-${MSYSTEM}/api/python

  mv -n config-default.toml config-default.toml.bak
  cat << EOF > config-default.toml
[lief.build]
type          = "Release"
cache         = true
ninja         = true
parallel-jobs = 0
extra-cmake-opt = [
  "-DBUILD_SHARED_LIBS=OFF",
  "-DCMAKE_SKIP_RPATH=ON",
  "-DLIEF_EXAMPLES=OFF",
  "-DLIEF_EXTERNAL_SPDLOG=ON",
  "-DLIEF_OPT_EXTERNAL_EXPECTED=ON",
  "-DLIEF_OPT_MBEDTLS_EXTERNAL=ON",
  "-DLIEF_OPT_NANOBIND_EXTERNAL=ON",
  "-DLIEF_OPT_NLOHMANN_JSON_EXTERNAL=ON",
  "-DLIEF_PYTHON_API=ON",
]

[lief.formats]
elf     = false
pe      = true
macho   = false
android = false
art     = false
vdex    = false
oat     = false
dex     = false

[lief.features]
json    = true
frozen  = true

[lief.logging]
enabled = true
debug   = false
EOF

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" api/python/dist/*.whl
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
